/**
 * 
 * Class that holds a tile.
 * 
 * @author Andrew
 * @creator Andrew
 * 
 * LAST UPDATED 2/4/12
 */

public class TileObject extends Object {
	// Tile to the right
	private TileObject rightNeighbour;
	
	// Tile to the left
	private TileObject leftNeighbour;
	
	// Tile on top
	private TileObject topNeighbour;
	
	// Tile on Bottom
	private TileObject bottomNeighbour;
	
	// Does the tile still exist in the game?
	private boolean exists;
	
	// Which tileset to use (currently only one, but if I added more it would be easier)
	private String tileset;
	
	// Is it marked? for random tile generation
	private boolean marked;
	
	// x,y and which tileset to use (in this case it is always g for ground (because they are brown))
  public TileObject(double x, double y,String tilesetinput) {
      super((new Sprite(tilesetinput + "t1.png")),x,y+200);
	  this.exists = true;
	  this.marked = false;
	  this.tileset = tilesetinput;
  }
  
  // Is it marked?
  public boolean getMarked() {
	  return this.marked;
  }
  
  // set the marked value
  public void setMarked(boolean b) {
	  this.marked = b;
  }
  
  // set the right tile
  public void setTileRight(TileObject n) {
	  this.rightNeighbour = n;
  }
  
  // left tile
  public void setTileLeft(TileObject n) {
	  this.leftNeighbour = n;
  }
  
  // tile on top
  public void setTileTop(TileObject n) {
	  this.topNeighbour = n;
  }
  
  // tile on bottom
  public void setTileBottom(TileObject n) {
	  this.bottomNeighbour = n;
  }
  
  // Does it exist?
  public boolean getExist() {
	  return this.exists;
  }
  
  // change its exist value
  public void setExist(boolean t) {
	  this.exists = t;
  }
  
  // return the tile on the right
  public TileObject getTileRight() {
	  return this.rightNeighbour;
  }
  
  // on the left
  public TileObject getTileLeft() {
	  return this.leftNeighbour;
  }
  
  // on the top
  public TileObject getTileTop() {
	  return this.topNeighbour;
  }
  
  // on the bottom
  public TileObject getTileBottom() {
	  return this.bottomNeighbour;
  }
  
  // reset its neighbours
  public void reset() {
	  this.leftNeighbour = null;
	  this.rightNeighbour = null;
	  this.topNeighbour = null;
	  this.bottomNeighbour = null;
  }
  // defines the explosion for ammo 2. If it has neighbours, blow them up
  public void explosion() {
	  if(haveRight()) { 
		  this.rightNeighbour.setExist(false);
		  this.rightNeighbour.update();
		  this.rightNeighbour.updateNeighbours();
	  }
	  if(haveLeft()) { 
		  this.leftNeighbour.setExist(false);
		  this.leftNeighbour.update();
		  this.leftNeighbour.updateNeighbours();
	  }
	  if(haveTop()) { 
		  this.topNeighbour.setExist(false);
		  this.topNeighbour.update();
		  this.topNeighbour.updateNeighbours();
	  }
	  if(haveBottom()) { 
		  this.bottomNeighbour.setExist(false);
		  this.bottomNeighbour.update();
		  this.bottomNeighbour.updateNeighbours();
	  }
	  setExist(false);
  }
  
  // does it have a tile on the right
  public boolean haveRight() {
	  if(this.rightNeighbour == null) return false;
	  else if(this.rightNeighbour.getExist() == false) return false;
	  else return true;
  }
  // on the left
  public boolean haveLeft() {
	  if(this.leftNeighbour == null) return false;
	  else if(this.leftNeighbour.getExist() == false) return false;
	  else return true;
  }
  // on top
  public boolean haveTop() {
	  if(this.topNeighbour == null) return false;
	  else if(this.topNeighbour.getExist() == false) return false;
	  else return true;
  }
  // on bottom
  public boolean haveBottom() {
	  if(this.bottomNeighbour == null) return false;
	  else if(this.bottomNeighbour.getExist() == false) return false;
	  else return true;
  }
  // draw it according to what its neighbours are. Gives the appearance of changing ground
  public void update() {
	if((haveTop()==false) &&
       (haveRight()==false) &&
       (haveBottom()==false) &&
       (haveLeft()==false)) {
		super.sprite = new Sprite(this.tileset + "t1.png");
	}
	
	else if((haveTop()==true) &&
		       (haveRight()==false) &&
		       (haveBottom()==false) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t2.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==true) &&
		       (haveBottom()==false) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t3.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==false) &&
		       (haveBottom()==true) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t4.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==false) &&
		       (haveBottom()==false) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t5.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==true) &&
		       (haveBottom()==false) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t6.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==false) &&
		       (haveBottom()==true) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t7.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==false) &&
		       (haveBottom()==false) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t8.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==true) &&
		       (haveBottom()==true) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t9.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==true) &&
		       (haveBottom()==false) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t10.png");
			}
	
	else if((haveTop()==false) &&
		       (haveRight()==false) &&
		       (haveBottom()==true) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t11.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==true) &&
		       (haveBottom()==true) &&
		       (haveLeft()==false)) {
				super.sprite = new Sprite(this.tileset + "t12.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==true) &&
		       (haveBottom()==false) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t13.png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==false) &&
		       (haveBottom()==true) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t14.png");
			}
	
	// since this tile appears alot I have alot of tiles for it, this just randomly picks one
	else if((haveTop()==false) &&
		       (haveRight()==true) &&
		       (haveBottom()==true) &&
		       (haveLeft()==true)) {
		        double n = Math.random();
		        int i = 0;
		        if(n>=0 && n<0.3) i = 1;
		        else if(n>=0.3 && n<0.6) i = 2;
		        else if(n>=0.6 && n<0.8) i = 3;
		        else i = 4;
				super.sprite = new Sprite(this.tileset + "t15(" + i + ").png");
			}
	
	else if((haveTop()==true) &&
		       (haveRight()==true) &&
		       (haveBottom()==true) &&
		       (haveLeft()==true)) {
				super.sprite = new Sprite(this.tileset + "t16.png");
			}
	
	else super.sprite = new Sprite(this.tileset + "t1.png");
	
  }
  // and update its neighbours
  public void updateNeighbours() {
	  if(haveRight()) getTileRight().update();
	  if(haveLeft()) getTileLeft().update();
	  if(haveTop()) getTileTop().update();
	  if(haveBottom()) getTileBottom().update();
  }
  
}